/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.cea.papyrus.core.editpart.IUMLElementEditPart;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.tabbedproperties.composites.TemplateParametersComposite;
import com.cea.papyrus.umlutils.Element;

/**
 * The template signature section on the element tab.
 * This section is added only when a TemplateableElement owns a TemplateSignature
 * (cf. TemplateSignatureSectionFilter)
 */
public class TemplateSignatureSection extends PropertyViewSection {

	/**
	 * 
	 */
	private TemplateParametersComposite templateParametersComposite;




	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		templateParametersComposite = new TemplateParametersComposite();
		templateParametersComposite.createContent(parent, tabbedPropertySheetPage);
	}

	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * 
	 */
	@Override
	public void refresh() {
		templateParametersComposite.refresh();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#setInput(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	/**
	 * 
	 * 
	 * @param part 
	 * @param selection 
	 */
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Assert.isTrue(selection instanceof IStructuredSelection);
		Object input = ((IStructuredSelection) selection).getFirstElement();
		if (input instanceof IUMLElementEditPart) {
			org.eclipse.uml2.uml.Element element = ((IUMLElementEditPart) input).getUmlElement();
        	if(element instanceof org.eclipse.uml2.uml.TemplateSignature) {
        		Element utilElement = new Element(((org.eclipse.uml2.uml.TemplateSignature)element).getTemplate());
        		templateParametersComposite.setElement(utilElement) ;
        		
        		// Commented out to remove dependency on classdiagram.
        		// @todo To be checked !!!
//        		if (!(input instanceof RedefinableTemplateSignatureTreeEditPart ||
//        			  input instanceof TemplateSignatureTreeEditPart))
//        			templateParametersComposite.setGraphNode(((GraphNodeEditPart)input).getGraphNode()); 
        	}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose
		();
		if(templateParametersComposite!=null)
			templateParametersComposite.dispose();
	}
}